#include "muduo/proto/dispatcher.h"
#include "muduo/proto/codec.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/base/CountDownLatch.h"
#include "muduo/net/EventLoopThread.h"

#include "request.pb.h"
#include <iostream>
#include <string>
#include <functional>

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;

    typedef std::shared_ptr<zy::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<zy::AddResponse> AddResponsePtr;
    Client(const std::string &sip, int sport) : _latch(1),
        _client(_loopthread.startLoop(), muduo::net::InetAddress(sip, sport), "Client"),
        _dispatcher(std::bind(&Client::onUnknownMessage, this, std::placeholders::_1,
            std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<zy::TranslateResponse>(std::bind(&Client::onTranslate, this,
                    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _dispatcher.registerMessageCallback<zy::AddResponse>(std::bind(&Client::onAdd, this,
                    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec,
                    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));
    }

    void connect()
    {
        _client.connect();
        _latch.wait();
    }
    void Translate(const std::string &msg)
    {
        zy::TranslateRequest req;
        req.set_msg(msg);
        _codec.send(_conn, req);
    }
    void Add(int num1, int num2)
    {
        zy::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        _codec.send(_conn, req);
    }

private:
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected())
        {
            _latch.countDown();
            _conn = conn;
        }
        else
        {
            // 连接关闭时的操作
            _conn.reset();
        }
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "翻译结果：" << message->msg() << std::endl;
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "加法结果：" << message->result() << std::endl;
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpConnectionPtr _conn;
    muduo::net::TcpClient _client;

    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};

int main()
{
    Client client("127.0.0.1", 8085);
    client.connect();

    client.Translate("hello");
    client.Add(11, 22);

    sleep(1);

    return 0;
}